package com.sen.common.widget;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

/**
 * HTML控件的style属性.
 * 
 * @author sen
 */
public class Style implements ToHtmlable {

	private Map<String, String> styles;
	private String style;

	public Style(String style) {
		this.style = style;
	}

	public Style(String styleName, String styleValue, String... others) {
		styles = new HashMap<String, String>();
		styles.put(styleName, styleValue);
		if (ArrayUtils.isEmpty(others)) {
			return;
		}
		int length = others.length;
		int even = length % 2 == 0 ? length : length - 1;
		for (int i = 0; i < even; i += 2) {
			styles.put(others[i], others[i + 1]);
		}
	}

	public Style(final Map<String, String> styles) {
		if (null == styles) {
			return;
		}
		this.styles = Collections.unmodifiableMap(styles);
	}

	public String toHtml() {
		if (MapUtils.isEmpty(styles)) {
			return StringUtils.EMPTY + StringUtils.defaultString(style);
		}
		StringBuffer html = new StringBuffer();
		for (Map.Entry<String, String> eachStyle : styles.entrySet()) {
			html.append(eachStyle.getKey()).append(":").append(eachStyle.getValue())
					.append(";");
		}
		return html.toString() + StringUtils.defaultString(style);
	}
}

